<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-8-3
 * Time: 下午12:40
 */

namespace admin\modules\rbac\forms;


use common\models\db\Menu;
use yii\base\Model;
use yii\db\Exception;

/**
 * Class RoleMenu
 * @package admin\modules\rbac\forms
 *
 * @property array $roleMenus
 */
class RoleMenu extends Model
{
    public $role_id;
    public $menus;
    public $role_menus;

    public function attributeLabels()
    {
        return [
            'role_id' => "角色",
            'menus' => "菜单",
            'role_users' => "用户菜单",
        ];
    }

    public function rules()
    {
        return [
            ['role_id', 'required'],
            ['menus', 'common\members\wodrow\validators\ArrayValidator'],
            ['role_menus', 'common\members\wodrow\validators\ArrayValidator'],
        ];
    }

    /**
     * @return array
     */
    public function getRoleCheckedInAllMenus()
    {
        /**
         * @var Menu[] $menus
         */
        $menus = Menu::find()->all();
        foreach ($menus as $k => $v){
            if ($v->root == $v->id)$menus[$k]->root = null;
        }
        $role_menus = $this->getCheckedRoleMenus();
        $info = [];
        foreach ($menus as $k => $v){
            if (in_array($v->id, $role_menus)){
                $info[] = [
                    'group' => $v->yii2App->name,
                    'name' => $v->name,
                    'value' => $v->id,
                    'checked' => true,
                    'parent' => $v->root,
                ];
            }else{
                $info[] = [
                    'group' => $v->yii2App->name,
                    'name' => $v->name,
                    'value' => $v->id,
                    'checked' => false,
                    'parent' => $v->root,
                ];
            }
        }
        return $info;
    }

    /**
     * @return array
     */
    public function getRoleMenus()
    {
        return [];
    }

    public function getCheckedRoleMenus()
    {
        $menus = \common\models\db\RoleMenu::find()->select(['menu_id'])->where(['role_id' => $this->role_id])->asArray()->all();
        $role_menus = array_column($menus, 'menu_id');
        return $role_menus?$role_menus:[];
    }

    public function saveRoleMenus()
    {
        $old = $this->getCheckedRoleMenus();
        $new = $this->role_menus;
        $add = array_diff($new, $old);
        $add_data = [];
        foreach ($add as $k => $v){
            $add_data[] = [
                'role_id' => $this->role_id,
                'menu_id' => $v,
            ];
        }
        $remove = array_diff($old, $new);
        $remove_data = \common\models\db\RoleMenu::find()->where(['role_id' => $this->role_id])->andWhere(['in', 'menu_id', $remove])->asArray()->all();
        $remove_data = array_column($remove_data, 'id');
        $trans = \Yii::$app->db->beginTransaction();
        try{
            \Yii::$app->db->createCommand()->batchInsert(\common\models\db\RoleMenu::tableName(), ['role_id', 'menu_id'], $add_data)->execute();
            if ($remove_data)\common\models\db\RoleMenu::deleteAll(['in', 'id', $remove_data]);
            \Yii::$app->session->addFlash("success", "保存成功");
            $trans->commit();
            return true;
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
    }
}